const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotkey: [],
    shidicon: ' icon-xiangxia',
    shname: '资产',
    shid: 3,
    itemList: ['资产', '供应', '求购'],
    itemListA: [{ name: '资产', shid: 3 }, { name: '供应', shid: 1 }, { name: '求购', shid: 2 }],
    clearbtn: false,
    keyWord: '',
    mykeyWord: wx.getStorageSync('mykeyWord')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var url = "api/FeijiuApp/GetSSHitList?strRSA=";
    app.getDaoGet(url, function (r) {
      if (r.code == 200) {
        that.setData({
          hotkey: r.data.ds
        })
      }
    })
  },
  actionSheetTap: function () {
    this.setData({
      shidicon: ' icon-xiangshang'
    })
    var that = this;
    wx.showActionSheet({
      itemList: that.data.itemList,
      success: function (e) {
        that.setData({
          shidicon: ' icon-xiangxia',
          shname: that.data.itemListA[e.tapIndex].name,
          shid: that.data.itemListA[e.tapIndex].shid,
        })
      },
      fail: function (e) {
        that.setData({
          shidicon: ' icon-xiangxia'
        })
      }
    })
  },
  Search: function (e) {
    this.setData({
      keyWord: e.detail.value
    })
    if (this.data.keyWord.length > 0) {
      this.setData({
        clearbtn: true
      })
    } else {
      this.setData({
        clearbtn: false
      })
    }
  },
  clearinput: function () {
    this.setData({
      keyWord: '',
      clearbtn: false
    })
  },
  searchBtn: function () {
    if (this.data.keyWord.length > 0) {
      this.toList();
      this.mykeyWord();
    }
  },
  hotKey: function (e) {
    this.setData({
      keyWord: e.currentTarget.dataset.keyy,
      clearbtn: true
    })
    this.toList();
  },
  //存本地搜索记录
  mykeyWord: function () {
    var mykey = wx.getStorageSync('mykeyWord');
    if (mykey) {
      if (mykey.indexOf(this.data.keyWord) >= 0) {
        mykey.splice(mykey.indexOf(this.data.keyWord), 1);
      }
      mykey.unshift(this.data.keyWord);
      if (mykey.length > 10) {
        mykey = mykey.slice(0, 10)
      }
      wx.setStorageSync('mykeyWord', mykey);
    } else {
      var arr = new Array(this.data.keyWord);
      wx.setStorageSync('mykeyWord', arr)
    }
  },
  //点击热搜词
  toList: function () {
    if (this.data.shid == 3) {
      wx.navigateTo({
        url: '/packageA/pages/zichanchuzhi/index?key=' + this.data.keyWord
      })
    } else {
      wx.navigateTo({
        url: '../gongqiuxinxi/index?key=' + this.data.keyWord + '&shid=' + this.data.shid
      })
    }
    this.mykeyWord();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //返回时更新历史
    var mykey = wx.getStorageSync('mykeyWord');
    if (mykey) {
      this.setData({
        mykeyWord: mykey
      })
    }
  },
  delmykey: function () {
    var that = this
    wx.showModal({
      title: 'Feijiu网提示您',
      content: '是否要删除全部历史搜索记录',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorageSync('mykeyWord');
          that.setData({
            mykeyWord: ''
          })
        }
      },

    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})